/* 
 * Db management functions
 * using Web Storage
 */

function dbController(){
    
    this.server = "http://aleonlinespot.altervista.org";
    
    //localStorage.clear();
 
    this.createDbIfNotExists = function(){
        if (!localStorage.photo) localStorage.photo = JSON.stringify(new Array());
        if (!localStorage.album) localStorage.album = JSON.stringify(new Array());
        if (!localStorage.latestAlbumIdx) localStorage.latestAlbumIdx = 0;
        if (!localStorage.latestPhotoIdx) localStorage.latestPhotoIdx = 0;
        if (!localStorage.contact) localStorage.contact = JSON.stringify(new Array());
    }

    this.getNewAlbumIdx = function(){
        var idx = parseInt(localStorage.latestAlbumIdx);
        localStorage.latestAlbumIdx = idx+1;
        return idx;
    }
    
    this.getNewPhotoIdx = function(){
        var idx = parseInt(localStorage.latestPhotoIdx);
        localStorage.latestPhotoIdx = idx+1;
        return idx;
    }
    
    this.saveAlbum = function(album){
        this.createDbIfNotExists();
        if (album.id == -1) album.id = this.getNewAlbumIdx();
        
        //get all albums
        var albums = this.getAlbums();
        //push new album
        albums[albums.length] = album;
        //save albums
        localStorage.album = JSON.stringify(albums);
        
        //DEBUG!!!!!!!!*************************************
//            var p = new photo("http://aleonlinespot.altervista.org/upload/949928560firefox.jpg", "DEBUG", "DEBUG", album.id, "DEBUG", "DEBUG");      
//            this.savePhoto(p);
        //DEBUG!!!!!!!!*************************************
    }
    
    this.getAlbums = function(){
        this.createDbIfNotExists();
        return JSON.parse(localStorage.album);
    }
    
    this.isExistingAlbum = function(name){
        var albums = this.getAlbums();
        for (var i=0; i<albums.length; i++){
            var a = albums[i];
            if (a.name == name) return true;
        }
        return false;
    }
    
    this.savePhoto = function(photo){
        this.createDbIfNotExists();
        if (photo.id == -1) photo.id = this.getNewPhotoIdx();
        var photos = this.getPhotos();
        photos[photos.length] = photo;
        localStorage.photo = JSON.stringify(photos);
    }
    
    this.getPhotos = function(){
        this.createDbIfNotExists();
        return JSON.parse(localStorage.photo);
    }
    
    this.isExistingPhotoInAlbum = function(path, albumId){
        var photos = this.getPhotos();
        for (var i=0; i<photos.length; i++){
            var p = photos[i];
            if (p.path == path && p.album == albumId) return true;
        }
        return false;
    }
    
    this.removePhoto = function(photoId){
        var photos = this.getPhotos();
        for (var i=0; i<photos.length; i++){
            var p = photos[i];
            if (p.id == photoId){
                photos.splice(i, 1);
                break;
            }
        }
        localStorage.photo = JSON.stringify(photos);
    }
    
    this.editPhoto = function(newPhoto){
        var photos = this.getPhotos();
        for (var i=0; i<photos.length; i++){
            var p = photos[i];
            if (p.id == newPhoto.id){
                photos.splice(i, 1, newPhoto);
                break;
            }
        }
        localStorage.photo = JSON.stringify(photos);
    }
    
    this.getPhotoList = function(albumId){
        var res = [];
        var photos = this.getPhotos();
        for (var i=0; i<photos.length; i++){
            var p = photos[i];
            if (p.album == albumId){
                res[res.length] = p;
            }
        }
        return res;
    }
    
    this.getAPhotoFromAlbum = function(albumId){
        var p = this.getPhotoList(albumId);
        if (p.length == 0) return null;
        return p[0];
    }
    
    this.getPhotoFromId = function(photoId){
        var photos = this.getPhotos();
        for (var i=0; i<photos.length; i++){
            var p = photos[i];
            if (p.id == photoId) return p;
        }
        return null;
    }
    
    this.getAlbumFromId = function(albumId){
        var albums = this.getAlbums();
        for (var i=0; i<albums.length; i++){
            var a = albums[i];
            if (a.id == albumId) return a;
        }
        return null;
    }
}
